//
//  SettingViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/15.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "SettingViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//    button.backgroundColor = [UIColor redColor];
//    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
     self.navigationItem.title = @"设置";

    
    self.settingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) style:UITableViewStylePlain];
    self.settingTableView.delegate = self;
    self.settingTableView.dataSource = self;
    [self.view addSubview:self.settingTableView];
    self.titleArray = [NSMutableArray arrayWithObjects:@"我的收藏", @"清楚缓存", @"夜间模式", @"联系我们", nil];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4)];
    imageV.image = [UIImage imageNamed:@"icon1024.png"];
    self.settingTableView.tableHeaderView = imageV;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
//        CollectViewController *collect = [[CollectViewController alloc]init];
//        [self.navigationController pushViewController:collect animated:YES];
        

        CollectionViewController *collectionT = [[CollectionViewController alloc] init];
        [self.navigationController pushViewController:collectionT animated:YES];
        
        
    }else if (indexPath.section ==1)
    {
        
        NSUInteger size = [[SDImageCache sharedImageCache]getSize];
        CGFloat mb = size/1024.0/1024.0;
        self.imageSize = [NSString stringWithFormat:@"缓存数据:%.2fMB",mb];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:self.imageSize message:@"是否清除缓存数据" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];

        
    }else if (indexPath.section==2)
    {
        

        
    }
    else if (indexPath.section==3)
    {

        
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"联系我们" message:@"\nEmail:bang_guo_hope365@163.com \n\n 开发者:SeanTeam  " delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertV show];
        
    }
    
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[SDImageCache sharedImageCache]cleanDisk];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *cachpath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0];
            NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachpath];
            
            for (NSString *p in files) {
                NSError *error;
                NSString *path = [cachpath stringByAppendingPathComponent:p];
                if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                }
            }
            [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];    });
  
    }
 
}
-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[SettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    if (indexPath.section ==0) {
        
       
        [cell writeCell:@"我的收藏"];
        
    }else if (indexPath.section==1)
    {
       
        [cell writeCell:@"清除缓存"];
        
    }else if (indexPath.section==2)
    {
       
        [cell writeCell:@"夜间模式"];
        cell.myDelegate = self;
       
    }else if (indexPath.section==3)
    {
        
        [cell writeCell:@"联系我们"];
        
    }
    return cell;
    
}
-(void)nightChange {
    [self.myDelegate nightChange];
    NSLog(@"1111");
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @" ";
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}

//-(void)buttonAction:(UIButton *)button {
//    
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    
//    
//    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
//    if ([@"on" isEqualToString:[userDefaults objectForKey:@"isNight"]]) {
//        
//        [userDefaults setObject:@"off" forKey:@"isNight"];
//        
//    } else {
//        [userDefaults setObject:@"on" forKey:@"isNight"];
//        
//    }
//    [notificationCenter postNotificationName:@"changeNight" object:@"on"];
//
//    
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
